texture: Add private can_load() function
authorBenjamin Otte <otte@redhat.com>
Tue, 14 Sep 2021 15:40:09 +0000 (17:40 +0200)
committerBenjamin Otte <otte@redhat.com>
Thu, 16 Sep 2021 22:25:22 +0000 (00:25 +0200)
... and use it to load textures in gtk_picture_set_from_file().

gdk/gdktexture.c
gdk/gdktextureprivate.h
gtk/gdkpixbufutils.c

index a33fc3347c7556fe710dafa9e7e398c7c757491d..a6e79bca56b5c37cc45a5117141f17750338a2c8 100644 (file)
@@ -410,6 +410,13 @@ gdk_texture_new_from_file (GFile   *file,
   return texture;
 }
 
+gboolean
+gdk_texture_can_load (GBytes *bytes)
+{
+  return gdk_is_png (bytes) ||
+         gdk_is_jpeg (bytes) ||
+         gdk_is_tiff (bytes);
+}
 
 /**
  * gdk_texture_new_from_bytes:
index f4cb5eab66c724ee2ae7f77e33d9d28c92a7af29..814ed5d92c502d26fb3fb39f5e29cd46e074b175 100644 (file)
@@ -35,6 +35,8 @@ struct _GdkTextureClass {
                                                          gsize                   stride);
 };
 
+gboolean                gdk_texture_can_load            (GBytes                 *bytes);
+
 GdkTexture *            gdk_texture_new_for_surface     (cairo_surface_t        *surface);
 cairo_surface_t *       gdk_texture_download_surface    (GdkTexture             *texture);
 /* NB: GdkMemoryTexture */
index 2b48d76943e0bfa220068869b75755f704782a30..7bbe5d5fd83161e1d63dbf5c0ac89ddc679ae313 100644 (file)
@@ -20,6 +20,8 @@
 #include "gdkpixbufutilsprivate.h"
 #include "gtkscalerprivate.h"
 
+#include "gdk/gdktextureprivate.h"
+
 static GdkPixbuf *
 load_from_stream (GdkPixbufLoader  *loader,
                   GInputStream     *stream,
@@ -597,42 +599,44 @@ GdkPaintable *
 gdk_paintable_new_from_bytes_scaled (GBytes *bytes,
                                      int     scale_factor)
 {
-  GdkPixbufLoader *loader;
-  GdkPixbuf *pixbuf = NULL;
   LoaderData loader_data;
   GdkTexture *texture;
   GdkPaintable *paintable;
 
   loader_data.scale_factor = scale_factor;
 
-  loader = gdk_pixbuf_loader_new ();
-  g_signal_connect (loader, "size-prepared",
-                    G_CALLBACK (on_loader_size_prepared), &loader_data);
-
-  if (!gdk_pixbuf_loader_write_bytes (loader, bytes, NULL))
-    goto out;
+  if (gdk_texture_can_load (bytes))
+    {
+      /* We know these formats can't be scaled */
+      texture = gdk_texture_new_from_bytes (bytes, NULL);
+      if (texture == NULL)
+        return NULL;
+    }
+  else
+    {
+      GdkPixbufLoader *loader;
+      gboolean success;
 
-  if (!gdk_pixbuf_loader_close (loader, NULL))
-    goto out;
+      loader = gdk_pixbuf_loader_new ();
+      g_signal_connect (loader, "size-prepared",
+                        G_CALLBACK (on_loader_size_prepared), &loader_data);
 
-  pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
-  if (pixbuf != NULL)
-    g_object_ref (pixbuf);
+      success = gdk_pixbuf_loader_write_bytes (loader, bytes, NULL);
+      /* close even when writing failed */
+      success &= gdk_pixbuf_loader_close (loader, NULL);
 
- out:
-  gdk_pixbuf_loader_close (loader, NULL);
-  g_object_unref (loader);
+      if (!success)
+        return NULL;
 
-  if (!pixbuf)
-    return NULL;
+      texture = gdk_texture_new_for_pixbuf (gdk_pixbuf_loader_get_pixbuf (loader));
+      g_object_unref (loader);
+    }
 
-  texture = gdk_texture_new_for_pixbuf (pixbuf);
   if (loader_data.scale_factor != 1)
     paintable = gtk_scaler_new (GDK_PAINTABLE (texture), loader_data.scale_factor);
   else
     paintable = g_object_ref ((GdkPaintable *)texture);
 
-  g_object_unref (pixbuf);
   g_object_unref (texture);
 
   return paintable;